﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>
  
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="SQL/text()">
    <xsl:if test=".!=' '">
      <TEXT>
        <xsl:call-template name="COLUMNS">
          <xsl:with-param name="COLUMNTEXT" select="."></xsl:with-param>
          <xsl:with-param name="KNOWNCOMMA" select="'0'"></xsl:with-param>
        </xsl:call-template>
      </TEXT>
    </xsl:if>
  </xsl:template>
  <xsl:template name="COLUMNS">
    <xsl:param name="COLUMNTEXT"></xsl:param>
    <xsl:param name="KNOWNCOMMA"></xsl:param>
    <xsl:choose>
      <xsl:when test="substring-after($COLUMNTEXT,',')!=''">
    <COLUMN>
    <xsl:value-of select="substring-before($COLUMNTEXT,',')"/>
    </COLUMN>
    <COMMA/>  
    <xsl:call-template name="COLUMNS">
      <xsl:with-param name="COLUMNTEXT" select="substring-after($COLUMNTEXT,',')"></xsl:with-param>
      <xsl:with-param name ="KNOWNCOMMA" select="'1'"></xsl:with-param>
    </xsl:call-template>
    </xsl:when>
      <xsl:when test="$KNOWNCOMMA='1'">
        <COLUMN>
          <xsl:value-of select="$COLUMNTEXT"/>
        </COLUMN>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$COLUMNTEXT"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>